﻿using System.Linq.Expressions;

namespace KingerRobot.PCRBW.SpiService;

public class TrussOrderImpl : ITrussOrderService
{
    private readonly static ITaskService _taskService = new WcsTaskImpl();
    private readonly static IMesOutOrdersRepositories _mesOutOrdersRepositories = new MesOutOrdersRepositories();


    #region 创建任务

    /// <summary>
    /// 创建任务
    /// </summary>
    /// <param name="equip_id">出库滑台名称</param>
    /// <param name="targetName">滑台编码</param>
    /// <returns></returns>
    public int CreateTask(long equip_id,string targetName)
    {
        return _taskService.CreateTruss(equip_id, targetName,(int)EnumMesOutOrder.ExWarehouseCompleted);
    }

    #endregion

    #region 任务执行
    /// <summary>
    /// 任务执行
    /// </summary>
    /// <param name="locId">库位</param>
    /// <param name="taskNo">任务号</param>
    /// <returns>-1未查询到任务 1任务更改成功；0 当前任务没有是创建的</returns>
    public int ExecuteTask(long locId, int taskNo)
    {
        // 更新任务指令状态
        var result = _taskService.UpdateTaskAndCmdStatus(locId, taskNo, (int)EnumTaskCmdStatus.Executed);
        if (result > 0)
        {
            return 1;
        }
        return -1;
    }

    #endregion

    #region 任务完成

    /// <summary>
    /// 任务完成
    /// </summary>
    public int FinishTask(int taskNo)
    {
        Expression<Func<POWcsTask, bool>> whereExpression = x => true;
        whereExpression = whereExpression.And(d => d.TaskNo == taskNo && d.TaskStatus==(int)EnumTaskStatus.Executed);
        var task = _taskService.QueryFrist(whereExpression);
        if (task is null || task.Task == null || task.Cmd == null)
        {
            WriteLog($"桁架任务》未查询到任务，任务号:[{taskNo}]", true);
            return 0;
        }

        if (task.Task.TaskType == (int)EnumTaskType.Truss)
        {
            MesOutOrder mesOutOrder = new MesOutOrder();
            mesOutOrder.Id = task.Task.OrderId.ToString();
            mesOutOrder.Status = (int)EnumMesOutOrder.TrussCompleted;
            mesOutOrder.TargetLoc = task.Task.TargetLoc;

            PORcsLocation rcsLocation = new PORcsLocation();
            rcsLocation.TaskId = null;
            rcsLocation.TaskOccupy = 0;
            rcsLocation.LocHasSku = 0;
            rcsLocation.LocHasSkuTime = null;
            rcsLocation.MaterialId = null;
            rcsLocation.ProductBatch = null;
            rcsLocation.TaskOccupyTime = null;
            rcsLocation.MdTime = null;
            rcsLocation.SkuRealCount = 0;
            rcsLocation.LocSize = 0;
            rcsLocation.Pid = null;
            rcsLocation.UpdateTime = DateTime.Now;

            MesProduct mesProduct = new MesProduct();
            mesProduct.Id = task.Task.MesId;
            mesProduct.Status = (int)EnumProductStatus.TrussComplete;

            _mesOutOrdersRepositories.ExecUpdateTrussStatusTran(mesOutOrder, mesProduct,rcsLocation);
            _taskService.UpdateTaskAndCmdStatus(task.Task.TargetLoc.GetValueOrDefault(), task.Task.TaskNo.GetValueOrDefault(), (int)EnumTaskStatus.Finish);
            return 1;
        }
        else
        {
            return 0;
        }
    }

    #endregion

    #region 获取桁架任务
    public List<TaskCmdAndLocDto> GetTrussTaskAndCmdBeanByCmdEquipId(long equipId)
    {
        return _taskService.GetTrussTaskAndCmdBeanByCmdEquipId(equipId);
    } 
    #endregion
}
